* regress growth on csr trying out all components of the Political Risk Index

cd "~/Documents/econ/research/current/cognitive/estimation/dofiles"

use ../data/growth_panel, clear
xtset isonum year, delta(5)

tab year if year>=1985, gen(yd) // create year dummies after 1985
drop yd1

local gvars "trade_open yr_sch asr csr_20 yd*"
local gmmlag 3
local xtabopts "robust twostep"
local outopts "se varlabel squarebrack drop(yd*) noautosumm"

local PRIvars "PRI GS SC IP IC EC C MIP RIP LO ET DA BQ"

// rescale subindices to be 0-100
//   indices scaled 0-12
foreach v in GS SC IP IC EC {
	replace `v' = `v'*100/12
}
//   indices scaled 0-6
foreach v in C MIP RIP LO ET DA {
	replace `v' = `v'*100/6
}
//   index scaled 0-4
replace BQ = BQ*100/4

local rPRI : word count `PRIvars'
matrix b = J(`rPRI', 2, .)
matrix se = J(`rPRI', 2, .)
local n = 1
foreach v in `PRIvars' {
	di "v = `v'"
	local slsh = cond(`n'<`rPRI', "\", "")
	local rtitl = `"`rtitl'"`:variable label `v''" `slsh' "'
	local gmm_rhs "gmm(lgdppw `v' trade_open yr_sch asr, laglimits(2 `gmmlag'))"
di `"xtabond2 gPWT5 lgdppw `v' `gvars', `gmm_rhs' `xtabopts'"'
	xtabond2 gPWT5 lgdppw `v' `gvars', `gmm_rhs' `xtabopts'
	matrix b[`n',1] = _b[`v']
	matrix b[`n',2] = _b[csr_20]
	matrix se[`n',1] = _se[`v']
	matrix se[`n',2] = _se[csr_20]
	local ++n
}
mata: p = 2*normal(-abs(st_matrix("b"):/st_matrix("se"))); ///
	st_matrix("stars", (p:<0.1) + (p:<0.05) + (p:<0.01) + (p:<0.001))
frmttable using ../../draft/images/PRI_trial, tex replace frag statmat(b) ///
	annotate(stars) asymbol("+", "*", "**", "***") sdec(3) rtitl(`rtitl') ///
	ctitl("Institutional index", "Institutional coefficient", "Child survival coefficient") ///
	note("* \(p < 0.05\); ** \(p < 0.01\)")

exit


// old code using Stata's new -collect- command
collect create PRI_trial, replace

local cmdnum = 1
foreach v in `PRIvars' {
	di "v = `v'"
	local xtabiv "iv(`v' trade_open yd*) twostep robust"
di `"xtabond2 gPWT5 lgdppw `v' `gvars', `xtabgmm' `pregmm' `xtabiv'"'
	collect: xtabond2 gPWT5 lgdppw `v' `gvars', `xtabgmm' `pregmm' `xtabiv'
	collect label values cmdset `cmdnum' "`:variable label `v''"
	local ++cmdnum
}

local PRIcoefs = `:word count `PRIvars''*"PRIcoef "
collect remap colname[`PRIvars'] = colname[`PRIcoefs']

collect stars _r_p 0.01 "**" 0.05 "*", attach(_r_b)
collect style cell result, nformat(%5.3f)
collect label levels colname csr_20 "Child Survival", modify
collect label levels colname PRIcoef "Institution", modify
collect layout (cmdset) (colname[csr_20 PRIcoef]#result[_r_b])
